package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.task.Task;
import com.zw.yzk.kabu.data.network.entity.GetShoppingCartEntity;
import com.zw.yzk.kabu.data.network.task.DeleteShoppingCartTask;
import com.zw.yzk.kabu.data.network.task.ModifyShoppingCartTask;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.mapper.ShoppingCartMapper;
import com.zw.yzk.kabu.view.view.ShoppingCartView;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by zhanwei on 2017/3/2.
 */

public class ShoppingCartPresenter implements Presenter {

    private UseCase getShoppingCart;
    private UseCase modifyShoppingCart;
    private UseCase deleteShoppingCart;
    private AppCompatActivity activity;
    private ShoppingCartView shoppingCartView;
    private ShoppingCartMapper mapper;

    @Inject
    public ShoppingCartPresenter(@Named("getShoppingCartUseCase") UseCase getShoppingCart,
                                 @Named("modifyShoppingCartUseCase") UseCase modifyShoppingCart,
                                 @Named("deleteShoppingCartUseCase") UseCase deleteShoppingCart,
                                 AppCompatActivity activity,
                                 ShoppingCartMapper mapper) {
        this.getShoppingCart = getShoppingCart;
        this.modifyShoppingCart = modifyShoppingCart;
        this.deleteShoppingCart = deleteShoppingCart;
        this.activity = activity;
        this.mapper = mapper;
    }

    public void setView(ShoppingCartView view) {
        this.shoppingCartView = view;
    }

    public void getShoppingCart() {
        getShoppingCart.execute(new GetShoppingCartSubscriber(activity), new Task());
    }

    public void modifyShoppingCart(List<ModifyShoppingCartTask.Product> productList) {
        modifyShoppingCart.execute(new ModifyShoppingCartSubscriber(activity), new ModifyShoppingCartTask(productList));
    }

    public void deleteShoppingCart(List<Integer> shoppingCartList) {
        deleteShoppingCart.execute(new DeleteShoppingCartSubscriber(activity), new DeleteShoppingCartTask(shoppingCartList));
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        getShoppingCart.unSubscribe();
        modifyShoppingCart.unSubscribe();
        deleteShoppingCart.unSubscribe();
    }

    private class ModifyShoppingCartSubscriber extends BaseSubscriber<Boolean> {

        public ModifyShoppingCartSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.modify_shopping_cart_failed);
        }
    }

    private class DeleteShoppingCartSubscriber extends BaseSubscriber<Boolean> {

        public DeleteShoppingCartSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.delete_shopping_cart_failed);
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }
    }

    private class GetShoppingCartSubscriber extends BaseSubscriber<GetShoppingCartEntity> {

        public GetShoppingCartSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            shoppingCartView.getShoppingCartFailed();
        }

        @Override
        public void onNext(GetShoppingCartEntity entity) {
            super.onNext(entity);
            shoppingCartView.getShoppingCartSuccess(mapper.transfer(entity));
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.get_shopping_cart_failed);
        }
    }
}
